<?php
define("EW_PAGE_ID", "add", TRUE); // Page ID
define("EW_TABLE_NAME", 'movie_tpm', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "movie_tpminfo.php" ?>
<?php include "userfn50.php" ?>
<?php include "usersinfo.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
$Security->LoadCurrentUserLevel('movie_tpm');
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
if (!$Security->CanAdd()) {
	$Security->SaveLastUrl();
	Page_Terminate("movie_tpmlist.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$movie_tpm->Export = @$_GET["export"]; // Get export parameter
$sExport = $movie_tpm->Export; // Get export parameter, used in header
$sExportFile = $movie_tpm->TableVar; // Get export file, used in header
?>
<?php

// Load key values from QueryString
$bCopy = TRUE;
if (@$_GET["No"] != "") {
  $movie_tpm->No->setQueryStringValue($_GET["No"]);
} else {
  $bCopy = FALSE;
}

// Create form object
$objForm = new cFormObj();

// Process form if post back
if (@$_POST["a_add"] <> "") {
  $movie_tpm->CurrentAction = $_POST["a_add"]; // Get form action
  LoadFormValues(); // Load form values
} else { // Not post back
  if ($bCopy) {
    $movie_tpm->CurrentAction = "C"; // Copy Record
  } else {
    $movie_tpm->CurrentAction = "I"; // Display Blank Record
    LoadDefaultValues(); // Load default values
  }
}

// Perform action based on action code
switch ($movie_tpm->CurrentAction) {
  case "I": // Blank record, no action required
		break;
  case "C": // Copy an existing record
   if (!LoadRow()) { // Load record based on key
      $_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
      Page_Terminate($movie_tpm->getReturnUrl()); // Clean up and return
    }
		break;
  case "A": // ' Add new record
		$movie_tpm->SendEmail = TRUE; // Send email on add success
    if (AddRow()) { // Add successful
      $_SESSION[EW_SESSION_MESSAGE] = "Add New Record Successful"; // Set up success message
      Page_Terminate($movie_tpm->KeyUrl($movie_tpm->getReturnUrl())); // Clean up and return
    } else {
      RestoreFormValues(); // Add failed, restore form values
    }
}

// Render row based on row type
$movie_tpm->RowType = EW_ROWTYPE_ADD;  // Render add type
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "add"; // Page id
var EW_SHOW_HIGHLIGHT = "Show highlight"; 
var EW_HIDE_HIGHLIGHT = "Hide highlight";

//-->
</script>
<script type="text/javascript">
<!--

function ew_ValidateForm(fobj) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_tpm_session"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - tpm session"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_tpm_session"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - tpm session"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_mv_ID"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - mv ID"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_mv_ID"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - mv ID"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_mv_hdd"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - mv hdd"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_mv_hdd"];
		if (elm && !ew_CheckNumber(elm.value)) {
			if (!ew_OnError(elm, "Incorrect floating point number - mv hdd"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_tpm_date"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - tpm date"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_tpm_date"];
		if (elm && !ew_CheckEuroDate(elm.value)) {
			if (!ew_OnError(elm, "Incorrect date, format = dd/mm/yyyy - tpm date"))
				return false; 
		}
	}
	return true;
}

//-->
</script>
<script type="text/javascript">
<!--
var ew_DHTMLEditors = [];

//-->
</script>
<link rel="stylesheet" type="text/css" media="all" href="calendar/calendar-win2k-1.css" title="win2k-1" />
<script type="text/javascript" src="calendar/calendar.js"></script>
<script type="text/javascript" src="calendar/lang/calendar-en.js"></script>
<script type="text/javascript" src="calendar/calendar-setup.js"></script>
<script type="text/javascript">
<!--
var ew_MultiPagePage = "Page"; // multi-page Page Text
var ew_MultiPageOf = "of"; // multi-page Of Text
var ew_MultiPagePrev = "Prev"; // multi-page Prev Text
var ew_MultiPageNext = "Next"; // multi-page Next Text

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker">Add to TABLE: movie tpm<br><br><a href="<?php echo $movie_tpm->getReturnUrl() ?>">Go Back</a></span></p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") { // Mesasge in Session, display
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
  $_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message in Session
}
?>
<form name="fmovie_tpmadd" id="fmovie_tpmadd" action="movie_tpmadd.php" method="post" onSubmit="return ew_ValidateForm(this);">
<p>
<input type="hidden" name="a_add" id="a_add" value="A">
<table class="ewTable">
  <tr class="ewTableRow">
    <td class="ewTableHeader">tpm session<span class='ewmsg'>&nbsp;*</span></td>
    <td<?php echo $movie_tpm->tpm_session->CellAttributes() ?>><span id="cb_x_tpm_session">
<input type="text" name="x_tpm_session" id="x_tpm_session"  size="30" value="<?php echo $movie_tpm->tpm_session->EditValue ?>"<?php echo $movie_tpm->tpm_session->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableAltRow">
    <td class="ewTableHeader">mv ID<span class='ewmsg'>&nbsp;*</span></td>
    <td<?php echo $movie_tpm->mv_ID->CellAttributes() ?>><span id="cb_x_mv_ID">
<input type="text" name="x_mv_ID" id="x_mv_ID"  size="30" value="<?php echo $movie_tpm->mv_ID->EditValue ?>"<?php echo $movie_tpm->mv_ID->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableRow">
    <td class="ewTableHeader">mv hdd<span class='ewmsg'>&nbsp;*</span></td>
    <td<?php echo $movie_tpm->mv_hdd->CellAttributes() ?>><span id="cb_x_mv_hdd">
<input type="text" name="x_mv_hdd" id="x_mv_hdd"  size="30" value="<?php echo $movie_tpm->mv_hdd->EditValue ?>"<?php echo $movie_tpm->mv_hdd->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableAltRow">
    <td class="ewTableHeader">tpm date<span class='ewmsg'>&nbsp;*</span></td>
    <td<?php echo $movie_tpm->tpm_date->CellAttributes() ?>><span id="cb_x_tpm_date">
<input type="text" name="x_tpm_date" id="x_tpm_date"  value="<?php echo $movie_tpm->tpm_date->EditValue ?>"<?php echo $movie_tpm->tpm_date->EditAttributes() ?>>
&nbsp;<img src="images/ew_calendar.gif" id="cx_tpm_date" alt="Pick a Date" style="cursor:pointer;cursor:hand;">
<script type="text/javascript">
Calendar.setup(
{
inputField : "x_tpm_date", // ID of the input field
ifFormat : "%d/%m/%Y", // the date format
button : "cx_tpm_date" // ID of the button
}
);
</script>
</span></td>
  </tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="    Add    ">
</form>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Load default values
function LoadDefaultValues() {
	global $movie_tpm;
}
?>
<?php

// Load form values
function LoadFormValues() {

	// Load from form
	global $objForm, $movie_tpm;
	$movie_tpm->tpm_session->setFormValue($objForm->GetValue("x_tpm_session"));
	$movie_tpm->mv_ID->setFormValue($objForm->GetValue("x_mv_ID"));
	$movie_tpm->mv_hdd->setFormValue($objForm->GetValue("x_mv_hdd"));
	$movie_tpm->tpm_date->setFormValue($objForm->GetValue("x_tpm_date"));
	$movie_tpm->tpm_date->CurrentValue = ew_UnFormatDateTime($movie_tpm->tpm_date->CurrentValue, 7);
}

// Restore form values
function RestoreFormValues() {
	global $movie_tpm;
	$movie_tpm->tpm_session->CurrentValue = $movie_tpm->tpm_session->FormValue;
	$movie_tpm->mv_ID->CurrentValue = $movie_tpm->mv_ID->FormValue;
	$movie_tpm->mv_hdd->CurrentValue = $movie_tpm->mv_hdd->FormValue;
	$movie_tpm->tpm_date->CurrentValue = $movie_tpm->tpm_date->FormValue;
	$movie_tpm->tpm_date->CurrentValue = ew_UnFormatDateTime($movie_tpm->tpm_date->CurrentValue, 7);
}
?>
<?php

// Load row based on key values
function LoadRow() {
	global $conn, $Security, $movie_tpm;
	$sFilter = $movie_tpm->SqlKeyFilter();
	if (!is_numeric($movie_tpm->No->CurrentValue)) {
		return FALSE; // Invalid key, exit
	}
	$sFilter = str_replace("@No@", ew_AdjustSql($movie_tpm->No->CurrentValue), $sFilter); // Replace key value

	// Call Row Selecting event
	$movie_tpm->Row_Selecting($sFilter);

	// Load sql based on filter
	$movie_tpm->CurrentFilter = $sFilter;
	$sSql = $movie_tpm->SQL();
	if ($rs = $conn->Execute($sSql)) {
		if ($rs->EOF) {
			$LoadRow = FALSE;
		} else {
			$LoadRow = TRUE;
			$rs->MoveFirst();
			LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$movie_tpm->Row_Selected($rs);
		}
		$rs->Close();
	} else {
		$LoadRow = FALSE;
	}
	return $LoadRow;
}

// Load row values from recordset
function LoadRowValues(&$rs) {
	global $movie_tpm;
	$movie_tpm->No->setDbValue($rs->fields('No'));
	$movie_tpm->tpm_session->setDbValue($rs->fields('tpm_session'));
	$movie_tpm->mv_ID->setDbValue($rs->fields('mv_ID'));
	$movie_tpm->mv_hdd->setDbValue($rs->fields('mv_hdd'));
	$movie_tpm->tpm_date->setDbValue($rs->fields('tpm_date'));
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $movie_tpm;

	// Call Row Rendering event
	$movie_tpm->Row_Rendering();

	// Common render codes for all row types
	// tpm_session

	$movie_tpm->tpm_session->CellCssStyle = "";
	$movie_tpm->tpm_session->CellCssClass = "";

	// mv_ID
	$movie_tpm->mv_ID->CellCssStyle = "";
	$movie_tpm->mv_ID->CellCssClass = "";

	// mv_hdd
	$movie_tpm->mv_hdd->CellCssStyle = "";
	$movie_tpm->mv_hdd->CellCssClass = "";

	// tpm_date
	$movie_tpm->tpm_date->CellCssStyle = "";
	$movie_tpm->tpm_date->CellCssClass = "";
	if ($movie_tpm->RowType == EW_ROWTYPE_VIEW) { // View row
	} elseif ($movie_tpm->RowType == EW_ROWTYPE_ADD) { // Add row

		// tpm_session
		$movie_tpm->tpm_session->EditCustomAttributes = "";
		$movie_tpm->tpm_session->EditValue = ew_HtmlEncode($movie_tpm->tpm_session->CurrentValue);

		// mv_ID
		$movie_tpm->mv_ID->EditCustomAttributes = "";
		$movie_tpm->mv_ID->EditValue = ew_HtmlEncode($movie_tpm->mv_ID->CurrentValue);

		// mv_hdd
		$movie_tpm->mv_hdd->EditCustomAttributes = "";
		$movie_tpm->mv_hdd->EditValue = ew_HtmlEncode($movie_tpm->mv_hdd->CurrentValue);

		// tpm_date
		$movie_tpm->tpm_date->EditCustomAttributes = "";
		$movie_tpm->tpm_date->EditValue = ew_HtmlEncode(ew_FormatDateTime($movie_tpm->tpm_date->CurrentValue, 7));
	} elseif ($movie_tpm->RowType == EW_ROWTYPE_EDIT) { // Edit row
	} elseif ($movie_tpm->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}

	// Call Row Rendered event
	$movie_tpm->Row_Rendered();
}
?>
<?php

// Add record
function AddRow() {
	global $conn, $Security, $movie_tpm;

	// Check for duplicate key
	$bCheckKey = TRUE;
	$sFilter = $movie_tpm->SqlKeyFilter();
	if (trim(strval($movie_tpm->No->CurrentValue)) == "") {
		$bCheckKey = FALSE;
	} else {
		$sFilter = str_replace("@No@", ew_AdjustSql($movie_tpm->No->CurrentValue), $sFilter); // Replace key value
	}
	if (!is_numeric($movie_tpm->No->CurrentValue)) {
		$bCheckKey = FALSE;
	}
	if ($bCheckKey) {
		$rsChk = $movie_tpm->LoadRs($sFilter);
		if ($rsChk && !$rsChk->EOF) {
			$_SESSION[EW_SESSION_MESSAGE] = "Duplicate value for primary key";
			$rsChk->Close();
			return FALSE;
		}
	}
	$rsnew = array();

	// Field tpm_session
	$movie_tpm->tpm_session->SetDbValueDef($movie_tpm->tpm_session->CurrentValue, 0);
	$rsnew['tpm_session'] =& $movie_tpm->tpm_session->DbValue;

	// Field mv_ID
	$movie_tpm->mv_ID->SetDbValueDef($movie_tpm->mv_ID->CurrentValue, 0);
	$rsnew['mv_ID'] =& $movie_tpm->mv_ID->DbValue;

	// Field mv_hdd
	$movie_tpm->mv_hdd->SetDbValueDef($movie_tpm->mv_hdd->CurrentValue, 0);
	$rsnew['mv_hdd'] =& $movie_tpm->mv_hdd->DbValue;

	// Field tpm_date
	$movie_tpm->tpm_date->SetDbValueDef(ew_UnFormatDateTime($movie_tpm->tpm_date->CurrentValue, 7), ew_CurrentDate());
	$rsnew['tpm_date'] =& $movie_tpm->tpm_date->DbValue;

	// Call Row Inserting event
	$bInsertRow = $movie_tpm->Row_Inserting($rsnew);
	if ($bInsertRow) {
		$conn->raiseErrorFn = 'ew_ErrorFn';
		$AddRow = $conn->Execute($movie_tpm->InsertSQL($rsnew));
		$conn->raiseErrorFn = '';
	} else {
		if ($movie_tpm->CancelMessage <> "") {
			$_SESSION[EW_SESSION_MESSAGE] = $movie_tpm->CancelMessage;
			$movie_tpm->CancelMessage = "";
		} else {
			$_SESSION[EW_SESSION_MESSAGE] = "Insert cancelled";
		}
		$AddRow = FALSE;
	}
	if ($AddRow) {
		$movie_tpm->No->setDbValue($conn->Insert_ID());
		$rsnew['No'] =& $movie_tpm->No->DbValue;

		// Call Row Inserted event
		$movie_tpm->Row_Inserted($rsnew);
	}
	return $AddRow;
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
